package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CityData;
import com.ruoyi.system.domain.SysCityData;
import com.ruoyi.system.service.ISysCityDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 城市管理Controller
 *
 * @author ruoyi
 * @date 2021-09-27
 */
@Controller
@RequestMapping("/system/cityData")
public class SysCityDataController extends BaseController {
    private String prefix = "system/cityData";

    @Autowired
    private ISysCityDataService sysCityDataService;

    @RequiresPermissions("system:cityData:view")
    @GetMapping()
    public String cityData() {
        return prefix + "/cityData";
    }


    /**
     * 查询城市管理列表
     */
    @RequiresPermissions("system:cityData:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysCityData sysCityData) {
        startPage();
        List<SysCityData> list = sysCityDataService.selectSysCityDataList(sysCityData);
        return getDataTable(list);
    }

    /**
     * 导出城市管理列表
     */
    @RequiresPermissions("system:cityData:export")
    @Log(title = "城市管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysCityData sysCityData) {
        List<SysCityData> list = sysCityDataService.selectSysCityDataList(sysCityData);
        ExcelUtil<SysCityData> util = new ExcelUtil<SysCityData>(SysCityData.class);
        return util.exportExcel(list, "城市管理数据");
    }

    /**
     * 新增城市管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存城市管理
     */
    @RequiresPermissions("system:cityData:add")
    @Log(title = "城市管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysCityData sysCityData) {
        return toAjax(sysCityDataService.insertSysCityData(sysCityData));
    }

    /**
     * 修改城市管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        SysCityData sysCityData = sysCityDataService.selectSysCityDataById(id);
        mmap.put("sysCityData", sysCityData);
        return prefix + "/edit";
    }

    /**
     * 修改保存城市管理
     */
    @RequiresPermissions("system:cityData:edit")
    @Log(title = "城市管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysCityData sysCityData) {
        return toAjax(sysCityDataService.updateSysCityData(sysCityData));
    }

    /**
     * 删除城市管理
     */
    @RequiresPermissions("system:cityData:remove")
    @Log(title = "城市管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysCityDataService.deleteSysCityDataByIds(ids));
    }

    /**
     * 加载城市管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        SysCityData cityData = new SysCityData();
        cityData.setLevel(2);
        List<Ztree> ztrees = sysCityDataService.selectSysCityDataTree(cityData);
        return ztrees;
    }

    /**
     * 加载城市管理树列表
     */
    @GetMapping("/jsonData")
    @ResponseBody
    public List<CityData> jsonData() {
        String cacheName = "cityData";
        List<CityData> list = new ArrayList<>();
        Object cache = CacheUtils.get(cacheName);

        if (cache != null) {
            list = (List<CityData>) cache;
            if (list.size() == 0) {
                CacheUtils.remove(cacheName);
                list = packageJson();
                CacheUtils.put(cacheName, list);
            }
        } else {
            list = packageJson();
            CacheUtils.put(cacheName, list);
        }
        return list;
    }

    private List<CityData> packageJson() {
        List<CityData> listP = new ArrayList<>();
        SysCityData paramP = new SysCityData();
        //获取省份
        paramP.setParentid(1);
        List<SysCityData> provinces = sysCityDataService.selectSysCityDataList(paramP);
        for (SysCityData province : provinces) {
            //获取城市
            List<CityData> listC = new ArrayList<>();
            SysCityData paramC = new SysCityData();
            paramC.setParentid(province.getId());
            List<SysCityData> cities = sysCityDataService.selectSysCityDataList(paramC);
            for (SysCityData city : cities) {
                //获取区域
                List<CityData> listA = new ArrayList<>();
                SysCityData paramA = new SysCityData();
                paramA.setParentid(city.getId());
                List<SysCityData> areas = sysCityDataService.selectSysCityDataList(paramA);
                for (SysCityData area : areas) {
                    CityData a = new CityData();
                    a.setN(area.getAreaname());
                    a.setCode(area.getId().toString());
                    listA.add(a);
                }
                CityData c = new CityData();
                c.setN(city.getAreaname());
                c.setCode(city.getId().toString());
                c.setS(listA);
                listC.add(c);
            }
            CityData p = new CityData();
            p.setN(province.getAreaname());
            p.setCode(province.getId().toString());
            p.setS(listC);
            listP.add(p);
        }
        return listP;
    }

}
